﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace APICUSTOMCONTROL
{
    public class InputUtils
    {
        public static bool IsDigit(String input, int minLen, int maxLen)
        {
            String pattern = @"^([0-9]*)$";
            Regex regex = new Regex(pattern);

            return regex.IsMatch(input)
                && input.Length <= maxLen
                && input.Length >= minLen;
        }

        public static bool IsEmail(String input, int minLen, int maxLen)
        {
            String pattern = @"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$";
            Regex regex = new Regex(pattern);

            return regex.IsMatch(input)
                && input.Length <= maxLen
                && input.Length >= minLen;
        }

        public static String ConvertToMD5Code(String source)
        {
            StringBuilder res = new StringBuilder();
            MD5 md5 = MD5.Create();
            byte[] input = System.Text.Encoding.UTF8.GetBytes(source);

            input = md5.ComputeHash(input);
            foreach (byte b in input)
            {
                res.Append(b.ToString("x2"));
            }

            return res.ToString();
        }

        public static bool CompareByMD5(String md5Source, String normal)
        {
            return md5Source == ConvertToMD5Code(normal);
        }
    }
}
